package Operation;

import Book.BookList;
import Book.book;

import java.util.Scanner;

public class AddOperation implements ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("添加图书！");
        //输入各方面你要添加的信息
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要添加的书名：");
        String name = scanner.nextLine();

        System.out.println("请输入要添加的书的作者：");
        String author = scanner.nextLine();

        System.out.println("请输入要添加的书的类型：");
        String type = scanner.nextLine();

        System.out.println("请输入要添加的书的价格：");
        int price = scanner.nextInt();


        //遍历书架来判断是否有相同书
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if(name.equals(bookList.getBooks(i).getName())){
                //有相同的书，直接退出
                System.out.println("不能添加重复的书籍！"+ "《"+ name + "》");
                return;
            }
        }


        //没有相同的书执行添加书操作，默认为最后一个
        bookList.setBooks(new book(name,author,type,price), bookList.getUsedSize());

        // 成功增添目标书，并且书架书总数加一
        bookList.setUsedSize(bookList.getUsedSize()+1);
        System.out.println("《"+ name + "》" + "增加成功");
    }
}
